package com.lesso.yjy.sunflower.infra.repository.impl;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.GsonBuilder;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.yjy.sunflower.domain.repository.EmployeeRepository;
import com.lesso.yjy.sunflower.infra.assembler.EmployeeAssembler;
import com.lesso.yjy.sunflower.infra.mapper.EmployeeMapper;
import com.lesso.yjy.sunflower.model.Employee;
import com.lesso.yjy.sunflower.pojo.dto.EmployeeDTO;
import com.lesso.yjy.sunflower.pojo.dto.EmployeeQueryDTO;
import com.lesso.yjy.sunflower.pojo.vo.EmployeeVO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Repository
public class EmployeeRepositoryImpl implements EmployeeRepository {
    private static final Gson GSON = new GsonBuilder().create();
    private final EmployeeMapper employeeMapper;

    private final StringRedisTemplate stringRedisTemplate;

    public EmployeeRepositoryImpl(EmployeeMapper employeeMapper, StringRedisTemplate stringRedisTemplate) {
        this.employeeMapper = employeeMapper;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public Long save(Employee employee) {
        employee.setDelStatus(0);
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String random = UUID.randomUUID().toString().substring(0, 4).toUpperCase();
        String employeeNo = timestamp + random;
        employee.setEmployeeNo(employeeNo);
        employeeMapper.insert(employee);
        return employee.getId();
    }

    @Override
    public Page<EmployeeVO> getPageList(EmployeeQueryDTO dto) {
        Page<EmployeeVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<Employee> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.hasText(dto.getName()), Employee.NAME, dto.getName());
        wrapper.between("entry_date", dto.getEntryDateStart(), dto.getEntryDateEnd());
        List<Employee> employees = employeeMapper.selectPage(page, wrapper);
        return page.setRecords(EmployeeAssembler.INSTANCE.entityListToVOList(employees));
    }

    @Override
    public Page<EmployeeVO> getCustomPageList(EmployeeQueryDTO dto) {
        Page<EmployeeVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<EmployeeVO> list = employeeMapper.selectPageList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public void removeById(Long id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public void updateById(EmployeeDTO dto) {
        Employee entity = EmployeeAssembler.INSTANCE.dtoToEntity(dto);
        employeeMapper.updateById(entity);
        stringRedisTemplate.delete("employee_" + dto.getId());
    }

    @Override
    public EmployeeVO getById(Long id) {
        // 先从缓存取
        String json = stringRedisTemplate.opsForValue().get("employee_" + id);
        if (StringUtils.hasText(json)) {
            return GSON.fromJson(json, EmployeeVO.class);
        }
        Employee employee = employeeMapper.selectById(id);
        EmployeeVO vo = EmployeeAssembler.INSTANCE.entityToVO(employee);
        stringRedisTemplate.opsForValue().set("employee_" + id, GSON.toJson(vo));
        return vo;
    }
}